class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = 0, n = nums.size();;
        for(auto& e : nums)
        {
            sum += e;
        }
        int a = (sum + target) / 2;
        if(a < 0 || (sum + target) % 2) // 小于0或者除不尽
            return 0;
 
        vector<int> dp(a + 1, 0);
        dp[0] = 1;
        for(int i = 1; i <= n; ++i)
        {
            for(int j = a; j >= nums[i - 1]; --j) // 滚动数组优化
            {
                dp[j] += dp[j - nums[i - 1]];
            }
        }
        return dp[a];
    }
};